import * as actions from '../actions/index';
import dayjs from 'dayjs';
import { repoTypes, repoActionTypes, groupTypes } from '../containers/auditConstants';

const allProjectTypeStr = repoTypes.join(',');

// 对页面prop 数据进行管理
export const initialState = {
  auditLogData: {},
  projects: [],
  groups: [],
  currentPage: 1,
  pageSize: 10,
  total: 0,
  tabKey: 'project',
  isLoadingTable: true,
  paramsData: {
    tb_user_ids: [],
    type: allProjectTypeStr,
    project_id: [],
    group_id: [],
    range: [null, null],
    start_time: null,
    end_time: null,
    entity_id: [],
    entity_type: '',
  },
  slsInfo: {
    authRedirectUrl: '',
    isSlsAuthLoading: true,
  },
};
const defaultAction = {
  type: 'doNothing',
};

export default function index(state = initialState, action = defaultAction) {
  let allType;
  let clearType;
  let entity_type;
  let audit_filter_type;
  switch (action.type) {
    case actions.UPDATE_DATA:
      return { ...state, ...action.data };
    case actions.RECEIVE_AUDIT_DATA: {
      const total = action.data[state.tabKey]?.shift().amount;
      return { ...state, auditLogData: action.data, total, currentPage: action.data.currentPage };
    }
    case actions.SET_TAB_KEY:
      if (action.data === 'project') {
        allType = allProjectTypeStr;
      }
      if (action.data === 'repo') {
        allType = repoActionTypes.join(',');
      }
      if (action.data === 'organization') {
        audit_filter_type = 'ALL';
      }
      if (action.data === 'group') {
        allType = groupTypes.join(',');
      }
      return {
        ...state,
        tabKey: action.data,
        paramsData: {
          ...state.paramsData,
          type: allType,
          audit_filter_type,
          entity_type,
        },
      };
    case actions.SET_CLEAR_DATA:
      if (action.data === 'project') {
        clearType = allProjectTypeStr;
      }
      if (action.data === 'repo') {
        clearType = repoActionTypes.join(',');
      }
      if (action.data === 'group') {
        clearType = groupTypes.join(',');
      }
      if (action.data === 'organization') {
        audit_filter_type = 'ALL';
      }
      return {
        ...state,
        paramsData: {
          ...state.paramsData,
          tb_user_ids: [],
          type: clearType,
          project_id: [],
          group_id: [],
          range: [null, null],
          start_time: null,
          end_time: null,
          entity_id: [],
          entity_type: '',
          audit_filter_type,
        },
      };
    case actions.SET_TBUSER_DATA:
      return {
        ...state,
        paramsData: {
          ...state.paramsData,
          tb_user_ids: action.data,
        },
      };
    case actions.SET_DATE_DATA:
      return {
        ...state,
        paramsData: {
          ...state.paramsData,
          range: action.data,
        },
      };
    case actions.RECEIVE_DATE_DATA:
      return {
        ...state,
        paramsData: {
          ...state.paramsData,
          start_time: dayjs(action.data[0]).format('YYYY-MM-DD'),
          end_time: dayjs(action.data[1]).format('YYYY-MM-DD'),
        },
      };
    case actions.SET_ENTITY_DATA:
      return {
        ...state,
        paramsData: {
          ...state.paramsData,
          entity_id: action.data,
        },
      };
    case actions.SET_TYPE_DATA:
      return {
        ...state,
        paramsData: {
          ...state.paramsData,
          type: action.data,
        },
      };
    case actions.SET_TYPE_FILTER_DATA:
      return {
        ...state,
        paramsData: {
          ...state.paramsData,
          audit_filter_type: action.data,
        },
      };
    case actions.SET_REPO_DATA:
      return {
        ...state,
        paramsData: {
          ...state.paramsData,
          project_id: action.data,
        },
      };
    case actions.SET_GROUP_DATA:
      return {
        ...state,
        paramsData: {
          ...state.paramsData,
          group_id: action.data,
        },
      };
    case actions.LOADING:
      return { ...state, isLoadingTable: action.data };
    case actions.RECEIVE_PROJECT:
      return { ...state, projects: action.data };
    case actions.RECEIVE_GROUP:
      return { ...state, groups: action.data };
    case actions.RESET_DATA:
      return { ...initialState, slsInfo: state.slsInfo, pageSize: state.pageSize };
    case actions.RESET_PROJECT:
      return {
        ...state,
        paramsData: {
          ...state.paramsData,
          project_id: [],
        },
      };
    case actions.SET_SLS_DATA:
      return {
        ...state,
        slsInfo: action.data,
      };
    default:
      return state;
  }
}
